<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:InDesign="adobe:ns:InDesign/" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <!-- ============================ -->
    <!-- importazione modelli communi -->
    <!-- ============================ -->

    <xsl:import href="../../common/daletbook/daletbook-lib.xsl"/>

    <!-- ======================== -->
    <!-- modelli blocchi di testo -->
    <!-- ======================== -->


    <!-- evidenziazioni -->

    <xsl:template match="highlight">
        <xsl:element name="TestoGrassetto">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="warning" mode="ind2">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Evidenziazione">
            <xsl:value-of select="$linebreak"/>
            <xsl:choose>
                <xsl:when test="attribute::logo = 'no'">
                    <xsl:element name="EvidenziazioneTesto">
                        <xsl:element name="TestoGrassetto">
                            <xsl:value-of select="concat($tab,$attenzione,':',$tab)"/>
                        </xsl:element>
                        <xsl:apply-templates select="para" mode="ind2"/>
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:element name="File">
                        <xsl:attribute name="href">
                            <xsl:value-of select="concat('file:///',$folder.pics,'/logo-attenzione.eps')"/>
                        </xsl:attribute>
                    </xsl:element>
                    <xsl:element name="EvidenziazioneTesto">
                        <xsl:element name="TestoGrassetto">
                            <xsl:value-of select="concat($tab,$tab,$attenzione,': ')"/>
                        </xsl:element>
                        <xsl:apply-templates select="para" mode="ind2"/>
                    </xsl:element>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:element>
    </xsl:template>

    <xsl:template match="warning" mode="ind4">
        <xsl:param name="item-label">
            <xsl:choose>
                <xsl:when test="attribute::logo = 'no'">
                    <xsl:element name="TestoGrassetto">
                        <xsl:value-of select="$attenzione"/>
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:element name="File">
                        <xsl:attribute name="href">
                            <xsl:value-of select="concat('file:///',$folder.pics,'/logo-attenzione.eps')"/>
                        </xsl:attribute>
                    </xsl:element>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:param>
        <xsl:param name="item-body">
            <xsl:element name="Paragrafo">
                <xsl:choose>
                    <xsl:when test="attribute::logo = 'no'">
                        <xsl:apply-templates/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:element name="TestoGrassetto">
                            <xsl:value-of select="concat($attenzione,': ')"/>
                        </xsl:element>
                        <xsl:apply-templates select="para" mode="ind4"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:element>
        </xsl:param>
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Evidenziazione">
            <xsl:attribute name="aid:table">
                <xsl:text>table</xsl:text>
            </xsl:attribute>
            <xsl:attribute name="aid:tcols">2</xsl:attribute>
            <xsl:attribute name="aid:trows">1</xsl:attribute>
            <xsl:call-template name="table-cell-ind4">
                <xsl:with-param name="colPosition">1</xsl:with-param>
                <xsl:with-param name="cellWidth" select="'14'"/>
                <xsl:with-param name="cellUnit">mm</xsl:with-param>
                <xsl:with-param name="cellContent" select="$item-label"/>
            </xsl:call-template>
            <xsl:call-template name="table-cell-ind4">
                <xsl:with-param name="colPosition">2</xsl:with-param>
                <xsl:with-param name="cellWidth" select="'65'"/>
                <xsl:with-param name="cellUnit">mm</xsl:with-param>
                <xsl:with-param name="cellContent" select="$item-body"/>
            </xsl:call-template>
        </xsl:element>
    </xsl:template>

    <xsl:template match="note">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Evidenziazione">
            <xsl:choose>
                <xsl:when test="attribute::logo = 'no' or not(attribute::logo)">
                    <xsl:element name="EvidenziazioneTesto">
                        <xsl:value-of select="concat($tab,$nota,':',$tab)"/>
                        <xsl:apply-templates/>
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:element name="File">
                        <xsl:value-of select="concat('file:///',$folder.pics,'/logo-nota.eps')"/>
                    </xsl:element>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:element>
    </xsl:template>

    <xsl:template match="example">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Evidenziazione">
            <xsl:element name="EvidenziazioneTesto">
                <xsl:value-of select="concat($tab,$esempio,':',$tab)"/>
                <xsl:apply-templates/>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <!-- paragraphs -->

    <xsl:template match="para">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Paragrafo">
            <xsl:call-template name="apply-locale"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="description/para">
        <xsl:if test="boolean(preceding-sibling::para)">
            <xsl:value-of select="$linebreak"/>
        </xsl:if>
        <xsl:element name="Paragrafo">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="warning/para" mode="ind2">
        <xsl:if test="boolean(preceding-sibling::para)">
            <xsl:value-of select="concat($linebreak,$tab,$tab)"/>
        </xsl:if>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="warning/para" mode="ind4">
        <xsl:if test="boolean(preceding-sibling::para)">
            <xsl:value-of select="$linebreak"/>
        </xsl:if>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="note/para|example/para">
        <xsl:if test="boolean(preceding-sibling::para)">
            <xsl:value-of select="concat($linebreak,$tab,$tab)"/>
        </xsl:if>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="item/para">
        <xsl:if test="boolean(preceding-sibling::para)">
            <xsl:value-of select="concat($linebreak,$tab,$tab)"/>
        </xsl:if>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="cell/para">
        <xsl:apply-templates/>
        <xsl:if test="boolean(following-sibling::para)">
            <xsl:value-of select="$linebreak"/>
        </xsl:if>
    </xsl:template>

    <xsl:template match="desc/para">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="label/para">
        <xsl:value-of select="$tab"/>
        <xsl:apply-templates/>
        <xsl:value-of select="concat(' -',$tab)"/>
    </xsl:template>

    <xsl:template match="symbol/para">
        <xsl:value-of select="$linebreak"/>
        <xsl:value-of select="$tab"/>
        <xsl:apply-templates/>
    </xsl:template>

    <!-- lists -->

    <xsl:template match="list">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Elenco">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="list/item">
        <xsl:variable name="itemlabel">
            <xsl:choose>
                <xsl:when test="ancestor::list/attribute::num[.='yes']">
                    <xsl:value-of select="$tab"/>
                    <xsl:number format="a -"/>
                    <xsl:value-of select="$tab"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat($tab,'-',$tab)"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="position() = 1">
                <xsl:value-of select="$itemlabel"/>
                <xsl:apply-templates/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$linebreak"/>
                <xsl:element name="ElencoElemento">
                    <xsl:value-of select="$itemlabel"/>
                    <xsl:apply-templates/>
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="sublist">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Elenco_Livello-2">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="sublist/item">
        <xsl:variable name="itemlabel">
            <xsl:choose>
                <xsl:when test="ancestor::sublist/attribute::num = 'yes'">
                    <xsl:value-of select="$tab"/>
                    <xsl:number format="a -"/>
                    <xsl:value-of select="$tab"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat($tab,'-',$tab)"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="position() = 1">
                <xsl:value-of select="$itemlabel"/>
                <xsl:apply-templates/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$linebreak"/>
                <xsl:element name="ElencoElemento_Livello-2">
                    <xsl:value-of select="$itemlabel"/>
                    <xsl:apply-templates/>
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- procedures -->

    <xsl:template match="procedure">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Procedura">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="procedure/item">
        <xsl:variable name="itemlabel">
            <xsl:choose>
                <xsl:when test="ancestor::procedure/attribute::num = 'no'">
                    <xsl:value-of select="concat($tab,'-',$tab)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$tab"/>
                    <xsl:number format="1 -"/>
                    <xsl:value-of select="$tab"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="position() = 1">
                <xsl:value-of select="$itemlabel"/>
                <xsl:apply-templates/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$linebreak"/>
                <xsl:element name="ProceduraElemento">
                    <xsl:value-of select="$itemlabel"/>
                    <xsl:apply-templates/>
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- mixed blocks -->

    <xsl:template match="description|warning|symbol">
        <!-- binary-table-style blocks -->
        <xsl:param name="binary-block" select="name()"/>
        <xsl:choose>
            <xsl:when test="$style.binary.table != '0' and $ns.AID &gt;= '4'">
                <xsl:apply-templates mode="ind4" select="self::node()[name() = $binary-block]"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates mode="ind2" select="self::node()[name() = $binary-block]"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="symbol" mode="ind4">
        <xsl:param name="item-label">
            <xsl:apply-templates select="picture"/>
        </xsl:param>
        <xsl:param name="item-body">
            <xsl:if test="boolean(child::label)">
                <xsl:element name="SimboloRiferimento">
                    <xsl:value-of select="child::label/child::para"/>
                </xsl:element>
            </xsl:if>
            <xsl:element name="SimboloDescrizione">
                <xsl:apply-templates select="para"/>
            </xsl:element>
        </xsl:param>
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Simbolo">
            <xsl:attribute name="aid:table">
                <xsl:text>table</xsl:text>
            </xsl:attribute>
            <xsl:attribute name="aid:tcols">2</xsl:attribute>
            <xsl:attribute name="aid:trows">1</xsl:attribute>
            <xsl:call-template name="table-cell-ind4">
                <xsl:with-param name="colPosition">1</xsl:with-param>
                <xsl:with-param name="cellWidth" select="'14'"/>
                <xsl:with-param name="cellUnit">mm</xsl:with-param>
                <xsl:with-param name="cellContent" select="$item-label"/>
            </xsl:call-template>
            <xsl:call-template name="table-cell-ind4">
                <xsl:with-param name="colPosition">2</xsl:with-param>
                <xsl:with-param name="cellWidth" select="'65'"/>
                <xsl:with-param name="cellUnit">mm</xsl:with-param>
                <xsl:with-param name="cellContent" select="$item-body"/>
            </xsl:call-template>
        </xsl:element>
    </xsl:template>

    <xsl:template match="symbol" mode="ind2">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Simbolo">
            <xsl:apply-templates select="picture"/>
            <xsl:value-of select="$tab"/>
            <xsl:if test="boolean(child::label)">
                <xsl:element name="SimboloRiferimento">
                    <xsl:value-of select="concat($tab,child::label/child::para)"/>
                </xsl:element>
            </xsl:if>
            <xsl:element name="SimboloDescrizione">
                <xsl:apply-templates select="para"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <xsl:template match="description" mode="ind2">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Descrizione">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="description" mode="ind4">
        <xsl:param name="item-label">
            <xsl:apply-templates select="node()[not(self::figure or self::picture)]"/>
        </xsl:param>
        <xsl:param name="item-body">
            <xsl:apply-templates select="child::figure|child::picture"/>
        </xsl:param>
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Descrizione">
            <xsl:attribute name="aid:table">
                <xsl:text>table</xsl:text>
            </xsl:attribute>
            <xsl:attribute name="aid:tcols">2</xsl:attribute>
            <xsl:attribute name="aid:trows">1</xsl:attribute>
            <xsl:call-template name="table-cell-ind4">
                <xsl:with-param name="colPosition">1</xsl:with-param>
                <xsl:with-param name="cellWidth" select="'82.5'"/>
                <xsl:with-param name="cellUnit">mm</xsl:with-param>
                <xsl:with-param name="cellContent" select="$item-label"/>
            </xsl:call-template>
            <xsl:call-template name="table-cell-ind4">
                <xsl:with-param name="colPosition">2</xsl:with-param>
                <xsl:with-param name="cellWidth" select="'82.5'"/>
                <xsl:with-param name="cellUnit">mm</xsl:with-param>
                <xsl:with-param name="cellContent" select="$item-body"/>
            </xsl:call-template>
        </xsl:element>
    </xsl:template>

    <!-- =========================== -->
    <!-- modelli di blocchi tabulari -->
    <!-- =========================== -->

    <xsl:template match="table">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Tabella">
            <xsl:choose>
                <xsl:when test="$ns.AID = '2'">
                    <xsl:apply-templates mode="ind2" select="self::table"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates mode="ind4" select="self::table"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:if test="boolean(child::title)">
                <xsl:value-of select="$linebreak"/>
                <xsl:element name="TabellaTitolo">
                    <xsl:value-of select="child::title"/>
                </xsl:element>
            </xsl:if>
        </xsl:element>
    </xsl:template>

    <!-- table model for InDesign 4 -->

    <xsl:template match="table" mode="ind4">
        <xsl:param name="tableWidth">
            <!-- FIXME: calculate the total width of the table -->
        </xsl:param>
        <xsl:param name="rowsNumber">
            <xsl:value-of select="count(child::row)"/>
        </xsl:param>
        <xsl:param name="columnsNumber">
            <xsl:value-of select="count(child::col)"/>
        </xsl:param>
        <xsl:attribute name="aid:table">
            <xsl:text>table</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="aid:tcols">
            <xsl:value-of select="$columnsNumber"/>
        </xsl:attribute>
        <xsl:attribute name="aid:trows">
            <xsl:value-of select="$rowsNumber"/>
        </xsl:attribute>
        <xsl:apply-templates mode="ind4" select="descendant::cell">
            <xsl:with-param name="rowsNumber" select="$rowsNumber"/>
            <xsl:with-param name="columnsNumber" select="$columnsNumber"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="row" mode="ind4">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="cell" mode="ind4">
        <xsl:param name="colPosition">
            <xsl:number from="row"/>
        </xsl:param>
        <xsl:param name="cellWidth" select="ancestor::table/col[position() = $colPosition]/@width"/>
        <xsl:param name="cellUnit">
            <xsl:choose>
                <!-- daletbook DTD v1.4 compatibility -->
                <xsl:when test="ancestor::table/col[position() = $colPosition]/@unit">
                    <xsl:value-of select="ancestor::table/col[position() = $colPosition]/@unit"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="ancestor::table/attribute::unit"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:param>
        <xsl:call-template name="table-cell-ind4">
            <xsl:with-param name="cellContent">
                <xsl:apply-templates/>
            </xsl:with-param>
            <xsl:with-param name="colPosition" select="$colPosition"/>
            <xsl:with-param name="cellWidth" select="$cellWidth"/>
            <xsl:with-param name="cellUnit" select="$cellUnit"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="table-cell-ind4">
        <xsl:param name="cellContent"/>
        <xsl:param name="colPosition"/>
        <xsl:param name="cellUnit"/>
        <xsl:param name="cellWidth"/>
        <!-- FIXME: colspan and rowspan
            ?which input for aid:ccols and aid:crows? -->
        <xsl:param name="colSpan" select="'1'"/>
        <xsl:param name="rowSpan" select="'1'"/>
        <xsl:element name="Cella">
            <xsl:attribute name="aid:table">
                <xsl:text>cell</xsl:text>
            </xsl:attribute>
            <xsl:attribute name="aid:ccols">
                <xsl:value-of select="$colSpan"/>
            </xsl:attribute>
            <xsl:attribute name="aid:crows">
                <xsl:value-of select="$rowSpan"/>
            </xsl:attribute>
            <xsl:attribute name="aid:ccolwidth">
                <xsl:call-template name="convertToPt">
                    <xsl:with-param name="value" select="$cellWidth"/>
                    <xsl:with-param name="unit" select="$cellUnit"/>
                </xsl:call-template>
            </xsl:attribute>
            <xsl:copy-of select="$cellContent"/>
        </xsl:element>
    </xsl:template>

    <!-- table model for InDesign 2 -->

    <xsl:template name="table" mode="ind2">
        <xsl:element name="InDesign:Table">
            <xsl:apply-templates mode="ind2" select="row"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="row" mode="ind2">
        <xsl:element name="InDesign:TR">
            <xsl:apply-templates select="cell"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="cell" mode="ind2">
        <xsl:element name="InDesign:TD">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <!-- ======================= -->
    <!-- modelli blocchi grafici -->
    <!-- ======================= -->
    
    <xsl:template match="picture[@filetype = 'swf']">
      <!-- InDesign non riconosce FLASH. Uscita = nulla.-->
    </xsl:template>

    <xsl:template match="picture">
      <xsl:param name="tipofile">
        <xsl:choose>
          <xsl:when test="@filetype = 'tif'">tif</xsl:when>
          <xsl:when test="@filetype = 'eps'">eps</xsl:when>
          <xsl:when test="@filetype = 'jpg'">jpg</xsl:when>
          <xsl:when test="@filetype = 'png'">png</xsl:when>
          <xsl:otherwise>eps</xsl:otherwise>
        </xsl:choose>
      </xsl:param>
        <xsl:if test="not(               parent::description or             parent::symbol or             parent::cell             )">
            <xsl:value-of select="$linebreak"/>
        </xsl:if>
        <xsl:element name="Immagine">
            <xsl:element name="File">
                <xsl:attribute name="href">
                    <xsl:value-of select="concat('file:///',$folder.images,'/',@filename,'.',$tipofile)"/>
                </xsl:attribute>
            </xsl:element>
        </xsl:element>
    </xsl:template>
    
    <xsl:template match="para/picture">
      <xsl:param name="tipofile">
        <xsl:choose>
          <xsl:when test="@filetype = 'tif'">tif</xsl:when>
          <xsl:when test="@filetype = 'eps'">eps</xsl:when>
          <xsl:when test="@filetype = 'jpg'">jpg</xsl:when>
          <xsl:when test="@filetype = 'png'">png</xsl:when>
          <xsl:otherwise>eps</xsl:otherwise>
        </xsl:choose>
      </xsl:param>
      <xsl:element name="File">
          <xsl:attribute name="href">
              <xsl:value-of select="concat('file:///',$folder.images,'/',@filename,'.',$tipofile)"/>
          </xsl:attribute>
      </xsl:element>
    </xsl:template>

    <xsl:template match="label/picture">
      <xsl:param name="tipofile">
        <xsl:choose>
          <xsl:when test="@filetype = 'tif'">tif</xsl:when>
          <xsl:when test="@filetype = 'eps'">eps</xsl:when>
          <xsl:when test="@filetype = 'jpg'">jpg</xsl:when>
          <xsl:when test="@filetype = 'png'">png</xsl:when>
          <xsl:otherwise>eps</xsl:otherwise>
        </xsl:choose>
      </xsl:param>
    <!-- moka: aggiunto in label picture per permettere di inserire una figura nella figdesc  -->
    <!-- aggiunto una catena di caratteri: $folder.pics = cartella di origine ,= in concat vuole dire aggiungi una maglia alla catena
        '/'= sottocartella 
        ,@filename = attributo necessario che individua con il nome del file la figura da inserire,
        '.',= punto di separazione del suffisso
        @filetype = estensione   -->
        <xsl:value-of select="$tab"/>
        <xsl:element name="file">
            <xsl:attribute name="href">
                <xsl:value-of select="concat('file:///',$folder.pics,'/',@filename,'.',$tipofile)"/>
            </xsl:attribute>
        </xsl:element>
        <xsl:value-of select="concat(' -',$tab)"/>
    </xsl:template>
    
    <xsl:template match="figure">
      <xsl:param name="tipofile">
        <xsl:choose>
          <xsl:when test="@filetype = 'tif'">tif</xsl:when>
          <xsl:when test="@filetype = 'eps'">eps</xsl:when>
          <xsl:when test="@filetype = 'jpg'">jpg</xsl:when>
          <xsl:when test="@filetype = 'png'">png</xsl:when>
          <xsl:otherwise>eps</xsl:otherwise>
        </xsl:choose>
      </xsl:param>
        <xsl:if test="not(parent::description or parent::symbol)">
            <xsl:value-of select="$linebreak"/>
        </xsl:if>
        <xsl:element name="Figura">
            <xsl:element name="File">
                <xsl:attribute name="href">
                    <xsl:value-of select="concat('file:///',$folder.images,'/',@figid,'.',$tipofile)"/>
                </xsl:attribute>
            </xsl:element>
            <xsl:value-of select="$linebreak"/>
            <xsl:element name="FiguraTitolo">
                <xsl:value-of select="$fig"/>
                <xsl:call-template name="figcounter"/>
                <xsl:if test="boolean(child::title) and $style.graphics-have-titles != '0'">
                    <xsl:value-of select="concat(': ',child::title)"/>
                </xsl:if>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <xsl:template match="figdesc">
        <xsl:value-of select="$linebreak"/>
            <xsl:element name="FiguraLegenda">
                <xsl:if test="boolean(child::title) and $style.graphics-have-titles != '0'">
                    <xsl:element name="LegendaTitolo">
                        <xsl:value-of select="child::title"/>
                    </xsl:element>
                </xsl:if>
                <xsl:for-each select="descitem">
                    <xsl:element name="FiguraLegendaElemento">
                        <xsl:apply-templates/>
                    </xsl:element>
                    <xsl:if test="boolean(following-sibling::descitem)">
                        <xsl:value-of select="$linebreak"/>
                    </xsl:if>
                </xsl:for-each>
            </xsl:element>
    </xsl:template>

    <xsl:template match="label/area">
        <xsl:value-of select="$tab"/>
        <xsl:element name="file">
            <xsl:attribute name="href">
                <xsl:value-of select="concat('file:///',$folder.pics,'/area-',@color,'.eps')"/>
            </xsl:attribute>
        </xsl:element>
        <xsl:value-of select="concat(' -',$tab)"/>
    </xsl:template>

    <!-- =============== -->
    <!-- modelli sezioni -->
    <!-- =============== -->

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="daletbook|book">
        <xsl:element name="Manuale">
            <!--
		<xsl:value-of select="$linebreak" />
        <xsl:element name="ManualeTitolo">
            <xsl:number />
            <xsl:value-of select="concat(' ',child::title)" />
        </xsl:element>
		-->
            <xsl:apply-templates select="node()[not(self::title)]"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="chapter">
        <xsl:element name="Capitolo">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="preface">
        <xsl:element name="Prefazione">
            <xsl:element name="SezioneTitolo_Livello-1">
                <xsl:value-of select="$prefazione"/>
            </xsl:element>
            <xsl:apply-templates select="node()[lang($lang)]"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="chapter/title">
        <!-- Bu:
            no way to get a float on top of the page with InDesign.
        	<xsl:element name="IntestazioneTitoloCapitolo">
                <xsl:value-of select="child::title" />
            </xsl:element>
    	-->
        <xsl:element name="CapitoloTitolo">
            <xsl:value-of select="$capitolo"/>
            <xsl:number count="chapter" format=" 1 :  "/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="sect1">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Sezione1">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="sect1/title">
        <xsl:element name="SezioneTitolo_Livello-1">
            <xsl:value-of select="$sezione"/>
            <xsl:number count="sect1" format=" 1 :  "/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="sect2">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Sezione2">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="sect2/title">
        <xsl:element name="SezioneTitolo_Livello-2">
            <xsl:number count="sect1" format="1."/>
            <xsl:number count="sect2"/>
            <xsl:value-of select="$tab"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="sect3">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Sezione3">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="sect3/title">
        <xsl:element name="SezioneTitolo_Livello-3">
            <xsl:number count="sect1" format="1."/>
            <xsl:number count="sect2" format="1."/>
            <xsl:number count="sect3"/>
            <xsl:value-of select="$tab"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="sect4">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Sezione4">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="sect4/title">
        <xsl:element name="SezioneTitolo_Livello-4">
            <xsl:number count="sect1" format="1."/>
            <xsl:number count="sect2" format="1."/>
            <xsl:number count="sect3" format="1."/>
            <xsl:number count="sect4"/>
            <xsl:value-of select="$tab"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="section">
        <xsl:value-of select="$linebreak"/>
        <xsl:element name="Sezione">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="section/title">
        <xsl:element name="SezioneTitolo_Livello-X">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <!-- =========== -->
    <!-- riferimenti -->
    <!-- =========== -->

    <xsl:template name="figcounter">
        <xsl:number count="chapter" format="1."/>
        <xsl:number count="figure" from="chapter" level="any"/>
    </xsl:template>

    <xsl:template match="ref.fignumber">
        <xsl:for-each select="id(attribute::ref)">
            <xsl:call-template name="figcounter"/>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="ref.figtitle">
        <xsl:value-of select="id(attribute::ref)/child::title"/>
    </xsl:template>

    <xsl:template match="ref.bookdata">
        <xsl:param name="info" select="attribute::ref"/>
        <xsl:value-of select="/descendant-or-self::daletbook/attribute::node()[name() = $info]"/>
    </xsl:template>

    <!-- =========== -->
    <!-- fine filtro -->
    <!-- =========== -->

</xsl:stylesheet>
